package com.controller;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.entity.Company;
import com.service.CompanyService;
import com.util.PageHelper;

//定义为控制器
@Controller
// 设置路径
@RequestMapping(value = "/company", produces = "text/plain;charset=utf-8")
public class CompanyController extends BaseController {
	// 注入Service 由于标签的存在 所以不需要getter setter
	@Autowired
	private CompanyService companyService;

	// 准备添加数据
	@RequestMapping("createCompany.action")
	public String createCompany() {
		return "admin/addcompany";
	}

	// 添加数据
	@RequestMapping("addCompany.action")
	public String addCompany(Company company) {
		company.setStatus("正常");
		this.companyService.insertCompany(company);
		return "redirect:/company/createCompany.action";
	}

	// 通过主键删除数据
	@RequestMapping("deleteCompany.action")
	public String deleteCompany(String id) {
		this.companyService.deleteCompany(id);
		return "redirect:/company/getAllCompany.action";
	}

	// 批量删除数据
	@RequestMapping("deleteCompanyByIds.action")
	public String deleteCompanyByIds() {
		String[] ids = this.getRequest().getParameterValues("companyid");
		for (String companyid : ids) {
			this.companyService.deleteCompany(companyid);
		}
		return "redirect:/company/getAllCompany.action";
	}

	// 更新数据
	@RequestMapping("updateCompany.action")
	public String updateCompany(Company company) {
		this.companyService.updateCompany(company);
		return "redirect:/company/getAllCompany.action";
	}

	@RequestMapping("status.action")
	public String status(String id) {
		String status = "锁定";
		Company company = this.companyService.getCompanyById(id);
		if (status.equals(company.getStatus())) {
			status = "正常";
		}
		company.setStatus(status);
		this.companyService.updateCompany(company);
		return "redirect:/company/getAllCompany.action";
	}

	// 显示全部数据
	@RequestMapping("getAllCompany.action")
	public String getAllCompany(String number) {
		List<Company> companyList = this.companyService.getAllCompany();
		PageHelper.getPage(companyList, "company", null, null, 10, number, this.getRequest(), null);
		return "admin/listcompany";
	}

	// 按条件查询数据 (模糊查询)
	@RequestMapping("queryCompanyByCond.action")
	public String queryCompanyByCond(String cond, String name, String number) {
		Company company = new Company();
		if (cond != null) {
			if ("username".equals(cond)) {
				company.setUsername(name);
			}
			if ("password".equals(cond)) {
				company.setPassword(name);
			}
			if ("companyname".equals(cond)) {
				company.setCompanyname(name);
			}
			if ("workdate".equals(cond)) {
				company.setWorkdate(name);
			}
			if ("scale".equals(cond)) {
				company.setScale(name);
			}
			if ("address".equals(cond)) {
				company.setAddress(name);
			}
			if ("tel".equals(cond)) {
				company.setTel(name);
			}
			if ("qq".equals(cond)) {
				company.setQq(name);
			}
			if ("status".equals(cond)) {
				company.setStatus(name);
			}
			if ("memo".equals(cond)) {
				company.setMemo(name);
			}
		}

		List<String> nameList = new ArrayList<String>();
		List<String> valueList = new ArrayList<String>();
		nameList.add(cond);
		valueList.add(name);
		PageHelper.getPage(this.companyService.getCompanyByLike(company), "company", nameList, valueList, 10, number, this.getRequest(),
				"query");
		name = null;
		cond = null;
		return "admin/querycompany";
	}

	// 按主键查询数据
	@RequestMapping("getCompanyById.action")
	public String getCompanyById(String id) {
		Company company = this.companyService.getCompanyById(id);
		this.getRequest().setAttribute("company", company);
		return "admin/editcompany";
	}

	public CompanyService getCompanyService() {
		return companyService;
	}

	public void setCompanyService(CompanyService companyService) {
		this.companyService = companyService;
	}

}

